package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.dao.mapper.BankCardBindMapper;
import com.xyy.saas.payment.dao.model.BankCardBind;
import com.xyy.saas.payment.dao.model.BankCardBindQueryParam;
import com.xyy.saas.payment.service.BankCardBindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 签约绑卡表(BankCardBind)表服务实现类
 *
 * @author chujian
 * @since 2023-06-08 14:48:49
 */
@Slf4j
@Service
public class BankCardBindServiceImpl implements BankCardBindService {
    @Resource
    private BankCardBindMapper bankCardBindMapper;

    /**
     * 通过ReqNo查询单条数据
     *
     * @param reqNo 请求唯一标识
     * @return 实例对象
     */
    @Override
    public BankCardBind queryByReqNo(String reqNo) {
        return this.bankCardBindMapper.queryByReqNo(reqNo);
    }

    /**
     * 查询单条数据
     * @param bankCardBind 实例对象
     * @return 实例对象
     */
    @Override
    public BankCardBind queryOne(BankCardBind bankCardBind) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        return bankCardBindMapper.queryOne(bankCardBind, null);
    }

    /**
     * 查询单条数据
     * @param bankCardBind 实例对象
     * @return 实例对象
     */
    @Override
    public BankCardBind queryOne(BankCardBind bankCardBind, List<Integer> status) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        return bankCardBindMapper.queryOne(bankCardBind, status);
    }

    /**
     * 查询总数
     * @param param 查询条件
     * @return
     */
    @Override
    public long total(BankCardBindQueryParam param) {
        if (null == param) {
            param = new BankCardBindQueryParam();
        }
        param.setCardNo(desenCardNo(param.getCardNo()));
        if (!StringUtils.isEmpty(param.getUpdateTimeEnd()) && param.getUpdateTimeEnd().trim().length() <= 10) {
            param.setUpdateTimeEnd(param.getUpdateTimeEnd().trim() + " 23:59:59");
        }
        return bankCardBindMapper.total(param);
    }

    /**
     * 查询指定行数据
     *
     * @param param 查询条件
     * @return 对象列表
     */
    @Override
    public List<BankCardBind> queryPage(BankCardBindQueryParam param) {
        if (null == param) {
            param = new BankCardBindQueryParam();
        }
        param.setCardNo(desenCardNo(param.getCardNo()));
        if (!StringUtils.isEmpty(param.getUpdateTimeEnd()) && param.getUpdateTimeEnd().trim().length() <= 10) {
            param.setUpdateTimeEnd(param.getUpdateTimeEnd().trim() + " 23:59:59");
        }
        param.setOffset(((param.getPage() == 0 ? 1 : param.getPage()) - 1) * param.getPageSize());
        return bankCardBindMapper.queryPage(param);
    }

    @Override
    public BankCardBind queryOneWithDesenCardNo(BankCardBind bankCardBind) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        BankCardBind result = bankCardBindMapper.queryOne(bankCardBind, null);
        desenCardNo(result);
        return result;
    }
    /**
     * 查询处理中的数据
     *
     * @return 实例对象
     */
    @Override
    public List<BankCardBind> queryProc() {
        return bankCardBindMapper.queryProc();
    }

    /**
     * 统计总行数
     *
     * @param bankCardBind 查询条件
     * @return 总行数
     */
    @Override
    public long count(BankCardBind bankCardBind) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        return bankCardBindMapper.count(bankCardBind);
    }

    /**
     * 新增数据
     *
     * @param bankCardBind 实例对象
     * @return 实例对象
     */
    @Override
    public BankCardBind insert(BankCardBind bankCardBind) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        this.bankCardBindMapper.insert(bankCardBind);
        return bankCardBind;
    }

    /**
     * 修改数据
     *
     * @param bankCardBind 实例对象
     * @return 实例对象
     */
    @Override
    public BankCardBind update(BankCardBind bankCardBind) {
        // 对银行卡号脱敏
        desenCardNo(bankCardBind);
        this.bankCardBindMapper.update(bankCardBind);
        return bankCardBind;
    }

    /**
     * 查询app端绑卡列表
     *
     * @param bankCardBind 实例对象
     * @return 实例对象
     */
    @Override
    public List<BankCardBind> appCardList(BankCardBind bankCardBind) {
        return bankCardBindMapper.appCardList(bankCardBind);
    }

    /**
     * 卡号脱敏 ：保留后四位，其余用*代替
     */
    private static void desenCardNo(BankCardBind bankCardBind) {
        if (null == bankCardBind) {
            return;
        }
        bankCardBind.setCardNo(desenCardNo(bankCardBind.getCardNo()));
    }
    private static String desenCardNo(String cardNo) {
        if (StringUtils.isEmpty(cardNo) || cardNo.length() <= 4) {
            return cardNo;
        }
        if (cardNo.startsWith("*")) {
            return cardNo;
        }
        StringBuilder cardNoDesen = new StringBuilder();
        for (int i = 0; i < cardNo.length() - 4; i++) {
            cardNoDesen.append("*");
        }
        cardNoDesen.append(cardNo.substring(cardNo.length() - 4));
        return cardNoDesen.toString();
    }

}
